package com.me.darkness_is_coming.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.me.darkness_is_coming.actors.MenuButton;
import com.me.darkness_is_coming.managers.Save;

public class ScoreScreen implements Screen
{
	private Stage stage;
	private Texture background_image;
	private SpriteBatch batch;
	private MenuButton back_button;
    private BitmapFont font;
    private BitmapFont font_stroke;
    private long[] highScores;

    private void DrawScores()
    {
        String score_note = "";
        float delta = 0.76f;
        for (int i = 0; i < 10; i++)
        {
            score_note = String.valueOf(i + 1) + ".      ";
            if (i < 9) score_note += " ";
            if (highScores[i] == 0) score_note += "  --- ";
            else score_note += String.valueOf(highScores[i]);
            font_stroke.draw(batch, score_note, Gdx.graphics.getWidth()*0.246f, Gdx.graphics.getHeight()*(delta - 0.004f));// - stroke_width/2);
            font.draw(batch, score_note, Gdx.graphics.getWidth()*0.25f, Gdx.graphics.getHeight()*delta);
            delta -= 0.055f;
        }
    }

	public ScoreScreen(SpriteBatch batch)
	{
        FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/CooperBlackStd.otf"));
        FreeTypeFontGenerator.FreeTypeFontParameter font_parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        font_parameter.size = (int)(Gdx.graphics.getWidth()*0.063f);
        font = gen.generateFont(font_parameter);
        font.setColor(240f/255f, 232f/255f, 3f/255f, 1.0f);
        font_stroke = gen.generateFont(font_parameter);
        font_stroke.setColor(74f/255f, 46f/255f, 5f/255f, 1.0f);

        //Save.init();
        Save.load();
        highScores = Save.game_data.getHighScores();
        Save.save();

		this.batch = batch;
		background_image = new Texture("data/Fons/ScoreFon.png");
        back_button = new MenuButton(new Texture("data/Menu/MenuButtonBack.png"), MenuButton.HOME, Gdx.graphics.getWidth()*0.238f, Gdx.graphics.getHeight()*0.117f, Gdx.graphics.getWidth()*0.523f, Gdx.graphics.getWidth()*0.083f);
        stage = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
        stage.addActor(back_button);
	}
	
	@Override
	public void render(float delta)
	{
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();
		batch.draw(background_image, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        DrawScores();
		batch.end();
		stage.act(delta);
		stage.draw();
	}

	@Override
	public void resize(int width, int height)
	{
        stage.getViewport().update(width, height, false);
	}

	@Override
	public void show()
	{
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void hide()
	{
		//
	}
	@Override
	public void pause()
	{
		//
	}
	@Override
	public void resume()
	{
		//
	}
	@Override
	public void dispose()
	{
        stage.dispose();
        background_image.dispose();
        font.dispose();
        back_button.dispose();
	}
}
